package com.umfb.edu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.umfb.commons.exception.OperateException;
import com.umfb.constant.State;
import com.umfb.demo.entity.edu.Photo;
import com.umfb.demo.service.edu.IPhotoService;
import com.umfb.edu.mapper.PhotoMapper;
import com.umfb.utils.IdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * Created by zcah on 18-7-19.
 */
@Service
public class PhotoServiceImpl implements IPhotoService {
    @Autowired
    private PhotoMapper photoMapper;
    @Override
    public List<Photo> findPhotoByAlbumId(String albumId, String orgId, int size) {
        return null;
    }

    @Override
    public PageInfo<Photo> findPhotoPage(String albumId,int pageSize, int pageNum) {
        if(StringUtils.isBlank(albumId)){
            return new PageInfo<>();
        }

        pageSize=pageSize==0?5:pageSize;

        Page<Object> page = PageHelper.startPage(pageNum, pageSize);
        List<Photo> photos=photoMapper.findPhotoByAlbumId(albumId,null);
        PageInfo<Photo> pageInfo = new PageInfo<>(photos);
        BeanUtils.copyProperties(page, pageInfo);
        return pageInfo;
    }

    @Override
    public String saveOrUpdate(Photo photo) throws OperateException {
        if(photo==null|| StringUtils.isBlank(photo.getAlbumId())||StringUtils.isBlank(photo.getPhotoName())){
            throw new OperateException("参数缺失");
        }

        Date cur = new Date();
        photo.setUpdateAt(cur);
        if(StringUtils.isBlank(photo.getPhotoId())){
            photo.setPhotoId(IdWorker.getIdWorker().nextId());
            photo.setCreateAt(cur);
            photo.setState(State.UNDELETE.getStateNum());
            photoMapper.insert(photo);
        }else{
            //gengxin
           // photoMapper.updateByPhotoIdSelective(photo);
        }
        return photo.getPhotoId();
    }

    @Override
    public void delPhoto(String photoId) throws OperateException {

    }

    @Override
    public List<Photo> randomHanYuBiHua(String albumId) {
        List<Photo> photos = photoMapper.randomHanYuBiHua(albumId);
        return photos;
    }


}
